package com.by_man.repository;

import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;

import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * 自定义的支持post请求的String类型的request
 * Created by man on 2017-02-15.
 */

public class PostStringRequest extends Request<String> {
    private final Response.Listener<String> mListener;
    private final Map<String,String> params;
    public PostStringRequest(int method, String url, Map<String, String> params, Response.Listener<String> listener,
                             Response.ErrorListener errorListener) {
        super(method,url,errorListener);
        mListener = listener;
        this.params = params;
    }

    @Override
    protected Map<String, String> getParams() throws AuthFailureError {
        return params;
    }

    @Override
    protected void deliverResponse(String response) {
        mListener.onResponse(response);
    }

    @Override
    protected Response<String> parseNetworkResponse(NetworkResponse response) {
        String parsed;
        try {
            parsed = new String(response.data, HttpHeaderParser.parseCharset(response.headers));
        } catch (UnsupportedEncodingException e) {
            parsed = new String(response.data);
        }
        return Response.success(parsed, HttpHeaderParser.parseCacheHeaders(response));
    }
}
